package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.ArticleDao;
import com.guochenglang.www.dao.CollectArticleDao;
import com.guochenglang.www.dao.CollectBaseDao;
import com.guochenglang.www.dao.CollectUserDao;
import com.guochenglang.www.pojo.*;
import com.guochenglang.www.service.CollectService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.MyDependency;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 收藏表业务
 *
 * @author GuoChenglang
 */
public class CollectServiceImpl implements CollectService {
    private static final CollectBaseDao COLLECT_BASE_DAO = MyDependency.returnObject(CollectBaseDao.class);
    private static final CollectArticleDao COLLECT_ARTICLE_DAO = MyDependency.returnObject(CollectArticleDao.class);
    private static final CollectUserDao COLLECT_USER_DAO = MyDependency.returnObject(CollectUserDao.class);

    /**
     * 收藏知识库
     *
     * @param baseId 知识库id
     * @param userId 用户id
     * @return 主键
     */
    @Affair
    @Override
    public int collectBase(Integer baseId, Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        CollectBase collectBase = new CollectBase(null, userId, baseId);
        //查看是否已经关注了并返回不同的num
        ArrayList<CollectBase> collectBases = COLLECT_BASE_DAO.collectBaseSelect(collectBase);
        if (collectBases.isEmpty()) {
            return COLLECT_BASE_DAO.insertCollectBase(collectBase);
        } else {
            return -1;
        }
    }

    /**
     * 获得收藏的知识库列表
     *
     * @param userId 用户id
     * @return 收藏的知识库列表
     */
    @Override
    public ArrayList<CollectBase> collectBases(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return COLLECT_BASE_DAO.collectBaseSelect(new CollectBase(null, userId, null));
    }

    /**
     * 取消收藏
     *
     * @param baseId 知识库id
     * @param userId 用户id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteCollectBase(Integer baseId, Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {

        return COLLECT_BASE_DAO.deleteCollectBase(new CollectBase(null, userId, baseId));
    }

    /**
     * 收藏文章
     *
     * @param articleId 文章id
     * @param userId    用户id
     * @return 主键
     */
    @Affair
    @Override
    public int collectArticle(Integer articleId, Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        CollectArticle collectArticle = new CollectArticle(userId, null, articleId);
        //查看是否已经收藏，根据结果返回不同的num
        ArrayList<CollectArticle> collectArticles = COLLECT_ARTICLE_DAO.collectArticleSelect(collectArticle);
        if (collectArticles.isEmpty()) {
            return COLLECT_ARTICLE_DAO.insertCollectArticle(collectArticle);
        } else {
            return -1;
        }
    }

    /**
     * 获得用户收藏的文章
     *
     * @param userId 用户id
     * @return 收藏的文章
     */
    @Override
    public ArrayList<CollectArticle> collectArticles(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        CollectArticle condition = new CollectArticle();
        condition.setUserId(userId);
        return COLLECT_ARTICLE_DAO.collectArticleSelect(condition);
    }

    /**
     * 取消收藏
     *
     * @param articleId 文章id
     * @param userId    用户id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteCollectArticle(Integer articleId, Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return COLLECT_ARTICLE_DAO.deleteCollectArticle(new CollectArticle(userId, null, articleId));
    }

    /**
     * 关注用户
     *
     * @param userId    用户id
     * @param collectId 关注的用户id
     * @return 主键
     */
    @Affair
    @Override
    public int collectUser(Integer userId, Integer collectId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        CollectUser collectUser = new CollectUser(null, collectId, userId);
        //查看是否已经收藏了，并根据结果返回不同的num
        ArrayList<CollectUser> collectUsers = COLLECT_USER_DAO.collectUserSelect(collectUser);
        if (collectUsers.isEmpty()) {
            return COLLECT_USER_DAO.insertCollectUser(collectUser);
        } else {
            return -1;
        }
    }

    /**
     * 获得关注的用户
     *
     * @param userId 用户id
     * @return 关注用户表
     */
    @Override
    public ArrayList<CollectUser> collectUsers(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return COLLECT_USER_DAO.collectUserSelect(new CollectUser(null, null, userId));
    }

    /**
     * 取消关注
     *
     * @param collectId 收藏的用户id
     * @param userId    用户id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteCollectUser(Integer collectId, Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return COLLECT_USER_DAO.deleteCollectUser(new CollectUser(null, collectId, userId));
    }

    /**
     * 获得关注用户的文章
     *
     * @param userId 收藏的用户id
     * @return 文章列表
     */
    @Override
    public ArrayList<Article> collectUserArticles(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得关注用户表
        ArrayList<CollectUser> collectUsers = COLLECT_USER_DAO.collectUserSelect(new CollectUser(null, null, userId));
        ArrayList<Article> articles = new ArrayList<>();
        //获得关注用户的文章
        if (collectUsers.isEmpty()) {
            return articles;
        }
        for (CollectUser collectUser : collectUsers) {
            Integer theyId = collectUser.getTheyId();
            Article condition = new Article();
            condition.setCreaterId(theyId);
            articles.addAll(MyDependency.returnObject(ArticleDao.class).articleSelect(condition));
        }
        return articles;
    }

    /**
     * 获得共同关注
     *
     * @param userId 第一个用户id
     * @param id     第二个用户id
     * @return 获得共同关注列表
     */
    @Override
    public ArrayList<CollectUser> ourCollectUser(Integer userId, Integer id) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得第一个用户的关注列表
        ArrayList<CollectUser> collectUsers1 = COLLECT_USER_DAO.collectUserSelect(new CollectUser(null, null, userId));
        //获得第二个用户的关注列表
        ArrayList<CollectUser> collectUsers2 = COLLECT_USER_DAO.collectUserSelect(new CollectUser(null, null, id));
        //若有空返回空集合
        if (collectUsers1.isEmpty() || collectUsers2.isEmpty()) {
            return new ArrayList<CollectUser>();
        }
        ArrayList<CollectUser> result = new ArrayList<>();
        //根据id获得用户
        for (CollectUser collectUser : collectUsers1) {
            for (CollectUser user : collectUsers2) {
                if (collectUser.getTheyId().equals(user.getTheyId())) {
                    result.add(user);
                }
            }
        }
        return result;
    }
}
